package com.example.zelonghu.test_all_without_network;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import Fragment.ControlFragment;
import Fragment.ShowFragment;
import java.util.ArrayList;

/**
 * @name MainActivity
 * @class name：AppCompatActivity
 * @class describe
 * @author zelonghu
 * @time 2019/4/12 11:13 PM
 * @change
 * @chang time
 * @class describe
 */
public class MainActivity extends AppCompatActivity {

    //define a viewpager for change page
    private ViewPager mViewPager;
    //define a navigation which's on the button of the layout
    private BottomNavigationView navigation;
    //associate the navigation and the viewpager,if the navigation changed, it affect the viewpage
    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                // change the view by the name of the menu from the item id
                case R.id.navigation_control:
                    Log.i("test","control layout");
                    mViewPager.setCurrentItem(0);
                    return true;
                case R.id.navigation_show:
                    mViewPager.setCurrentItem(1);
                    Log.i("test","show layout");
                    return true;

            }
            return false;
        }
    };

    /**
     * @author zelonghu
     * @time 2019/4/12  11:01 PM
     * @describe do when it is created
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.i("test","on create");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //bind the logic object with the real object
        navigation = (BottomNavigationView) findViewById(R.id.navigation);
        mViewPager=(ViewPager) findViewById(R.id.mViewPager);
        //link listenser
        Log.i("test","add ItemSelectedListener");
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                //when it scroll to another page, the navigator should change with it
                navigation.getMenu().getItem(position).setChecked(true);
            }


            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }

        });
        //define a list to hold all the fragmen
        final ArrayList<Fragment> fgLists = new ArrayList<>(2);
        fgLists.add(new ControlFragment());
        fgLists.add(new ShowFragment());


        //difine the adaptor for the viewpager,it links the page with the fragments
        FragmentPagerAdapter mPagerAdapter=new FragmentPagerAdapter(getSupportFragmentManager()) {
            // 2 method should be weitten, all of them should link with List<Fragment> ,like this
            @Override
            public Fragment getItem(int position) {
                return fgLists.get(position);  //return Fragment
            }

            @Override
            public int getCount() {
                return fgLists.size();  //size
            }
        };
        //link the adapter to the page
        mViewPager.setAdapter(mPagerAdapter);
        mViewPager.setOffscreenPageLimit(2);//预先加载两页
    }
}
